package bc

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/bc"
	bcReq "github.com/flipped-aurora/gin-vue-admin/server/model/bc/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type BcProjectApi struct {
}

var bcProjectService = service.ServiceGroupApp.BcServiceGroup.BcProjectService

// CreateBcProject 创建bcProject表
// @Tags BcProject
// @Summary 创建bcProject表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcProject true "创建bcProject表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /bcProject/createBcProject [post]
func (bcProjectApi *BcProjectApi) CreateBcProject(c *gin.Context) {
	var bcProject bc.BcProject
	err := c.ShouldBindJSON(&bcProject)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	//datas, _ := bcProjectService.GetBcProjectAll()
	//maxProjectID := 0
	//for i := 0; i < len(datas); i++ {
	//	if int(datas[i].ID) > maxProjectID {
	//		maxProjectID = int(datas[i].ID)
	//	}
	//}
	//maxProjectID++
	//bcProject.ProjectNo = strconv.Itoa(maxProjectID)
	//fmt.Println("maxProjectID:" + strconv.Itoa(maxProjectID))
	if err := bcProjectService.CreateBcProject(&bcProject); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteBcProject 删除bcProject表
// @Tags BcProject
// @Summary 删除bcProject表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcProject true "删除bcProject表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /bcProject/deleteBcProject [delete]
func (bcProjectApi *BcProjectApi) DeleteBcProject(c *gin.Context) {
	var bcProject bc.BcProject
	err := c.ShouldBindJSON(&bcProject)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bcProjectService.DeleteBcProject(bcProject); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteBcProjectByIds 批量删除bcProject表
// @Tags BcProject
// @Summary 批量删除bcProject表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除bcProject表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /bcProject/deleteBcProjectByIds [delete]
func (bcProjectApi *BcProjectApi) DeleteBcProjectByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bcProjectService.DeleteBcProjectByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateBcProject 更新bcProject表
// @Tags BcProject
// @Summary 更新bcProject表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcProject true "更新bcProject表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /bcProject/updateBcProject [put]
func (bcProjectApi *BcProjectApi) UpdateBcProject(c *gin.Context) {
	var bcProject bc.BcProject
	err := c.ShouldBindJSON(&bcProject)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bcProjectService.UpdateBcProject(bcProject); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindBcProject 用id查询bcProject表
// @Tags BcProject
// @Summary 用id查询bcProject表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query bc.BcProject true "用id查询bcProject表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /bcProject/findBcProject [get]
func (bcProjectApi *BcProjectApi) FindBcProject(c *gin.Context) {
	var bcProject bc.BcProject
	err := c.ShouldBindQuery(&bcProject)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if rebcProject, err := bcProjectService.GetBcProject(bcProject.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"rebcProject": rebcProject}, c)
	}
}

func (bcProjectApi *BcProjectApi) GetProjectList(c *gin.Context) {

	if bcProjects, err := bcProjectService.GetBcProjectAll(); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {

		response.OkWithData(gin.H{"rebcProject": bcProjects}, c)
	}
}

// GetBcProjectList 分页获取bcProject表列表
// @Tags BcProject
// @Summary 分页获取bcProject表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query bcReq.BcProjectSearch true "分页获取bcProject表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /bcProject/getBcProjectList [get]
func (bcProjectApi *BcProjectApi) GetBcProjectList(c *gin.Context) {
	var pageInfo bcReq.BcProjectSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	data, err := utils.GetClaims(c)
	if err != nil {
		response.FailWithMessage("获取失败", c)
		return
	}
	user, err := userService.GetUserInfo(data.UUID)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list := make([]int, 0)
	switch data.AuthorityId {
	case 1:
		// 查一下用户对应的projectId
		list = append(list, user.ProjectId)
	}
	if list, total, err := bcProjectService.GetBcProjectInfoList(pageInfo, list); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		pList := make([]bc.BcProject, 0)
		for i := 0; i < len(list); i++ {
			if data.AuthorityId == 1 {
				list[i].CustomerPrice = user.CustomerPrice
			}
			data, err := bcPhoneService.GetBcPhoneList(int(list[i].ID))
			if err != nil {
				global.GVA_LOG.Error("查询失败!", zap.Error(err))
			}
			*list[i].RemainNum = len(data)
			pList = append(pList, list[i])
		}
		response.OkWithDetailed(response.PageResult{
			List:     pList,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
